package gui.frame;

import java.awt.Canvas;
import java.awt.Dialog;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;

class ToeDialog extends Dialog{
	static final int BLANK=0;
	static final int XX=1;
	static final int OO=2;
	
	int turn=XX;
	
	public ToeDialog(Frame parent,int w,int h){
		super(parent,"The game itself",false);
		this.setLayout(new GridLayout(w,h));
		for(int i=0;i<w*h;i++)
			this.add(new FrameButton(this));
		resize(w*50,h*50);
	}
	
	public boolean handleEvent(Event evt){
		if(evt.id==Event.WINDOW_DESTROY)
		{
			ToeDialog.this.dispose();
		}			
		else
			return super.handleEvent(evt);
		return true;
		
	}
}

public class FrameButton extends Canvas{

	int state=ToeDialog.BLANK;
	ToeDialog parent;
	FrameButton(ToeDialog parent){
		this.parent=parent;
	}
	
	public void paint(Graphics g){
		int x1=0,x2=size().width-1;
		int y1=0,y2=size().height-1;
		
		g.drawRect(x1, y1, x2, y2);
		x1=x2/4;
		y1=y2/4;
		
		int width=x2/2;
		int height=y2/2;
		
		if(state==ToeDialog.XX){
			g.drawLine(x1, y1, x1+width, y1+height);
			g.drawLine(x1, y1+height, x1+width, y1);
		}
		if(state==ToeDialog.OO){
			g.drawOval(x1, y1, x1+width/2, y1+height/2);
		}
	}
	public boolean mouseDown(Event evt,int x,int y){
		if(state==ToeDialog.BLANK){
			state=parent.turn;
			parent.turn=(parent.turn==ToeDialog.XX?ToeDialog.OO:ToeDialog.XX);
		}else
			state=(parent.turn==ToeDialog.XX?ToeDialog.OO:ToeDialog.XX);
		repaint();
		return true;
	}
}
